import { defineStore } from 'pinia'
import request from '@/utils/request'
import {
  ApiRes,
  BannerItem,
  NewListItem,
  HotListItem,
  BrandListItem,
  GoodsListItem,
  SpecialListItem
} from '@/types/data'
export default defineStore('home', {
  state () {
    return {
      // 轮播图数据列表
      bannerList: [] as BannerItem[],
      // 新鲜好物数据
      newList: [] as NewListItem[],
      hotList: [] as HotListItem[], // 人气推荐数据
      brandList: [] as BrandListItem[], // 热门品牌
      goodsList: [] as GoodsListItem[], // 商品区块
      specialList: [] as SpecialListItem[] // 最新专题
    }
  },
  actions: {
    // 轮播图数据
    async setBannerList () {
      if (this.bannerList.length) {
        return
      }
      const res = await request.request<ApiRes<BannerItem[]>>({
        url: '/home/banner'
      })
      //   console.log(res)
      this.bannerList = res.data.result
    },

    // 新鲜好物数据
    async homeNew () {
      const res = await request.request<ApiRes<NewListItem[]>>({
        url: '/home/new'
      })
      this.newList = res.data.result
      // console.log(res)
    },

    // 人气推荐
    async homeHot () {
      const res = await request.request<ApiRes<HotListItem[]>>({
        url: '/home/hot'
      })
      // console.log(res)
      this.hotList = res.data.result
    },

    // 热门品牌
    async homeBrand () {
      const res = await request.request<ApiRes<BrandListItem[]>>({
        url: '/home/brand'
      })
      this.brandList = res.data.result
    },

    // 商品区块
    async homeGoods () {
      const res = await request.request<ApiRes<GoodsListItem[]>>({
        url: '/home/goods'
      })
      this.goodsList = res.data.result
    },

    // 最新专题
    async homeSpecial () {
      const res = await request.request<ApiRes<SpecialListItem[]>>({
        url: '/home/special'
      })
      // console.log(res)
      this.specialList = res.data.result
    }
  }
})
